<?php namespace Admin\Model;

use Hdphp\Model\Model;

class GoodsAttr extends Model{

	//数据表
	protected $table = "shop_goods_attr";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=array(
		//字段名: 表单字段名
		//验证方法: 函数或模型方法
		//验证条件: 1有字段时验证(默认)	2值不为空时验证  	3必须验证
		//验证时间: 1 插入时验证		2更新时空时验证 	3全部情况验证 (默认)
		//array('字段名','验证方法','提示信息',验证条件,验证时间),


		);


	//自动完成
	protected $auto=array(
		//字段名: 表单字段名
		//处理方法: 函数或模型方法
		//方法类型: string(字符串 默认)  function(函数)  method(模型方法)
		//验证条件: 1有字段时处理(默认)	2值不为空时 3必须处理
		//处理时间: 1 插入时  2更新时 3全部情况 (默认)
		//array('字段名','处理方法','方法类型',验证条件,验证时间),


		);

	// public function addGoodsAttr($goods_id)
	// {
	// 	foreach($_POST['attr_id'] as $k=>$v)
	// 	{

	// 		$data['goods_id']=$goods_id;
	// 		$data['attr_values']=trim($_POST['attr_values'][$k]);
	// 		$data['attr_id']=$_POST['attr_id'][$k];
	// 		$data['add_price']=(int)$_POST['add_price'][$k];
	// 		$this->add($data);
	// 	}
	// 	return true;
	// }

	// public function editGoodsAttr($goods_id)
	// {
	// 	foreach($_POST['attr_id'] as $k=>$v)
	// 	{

	// 		$data['goods_id']=$_POST['goods_id'];
	// 		$data['attr_values']=$_POST['attr_values'][$k];
	// 		$data['good_attr_id']=$_POST['good_attr_id'][$k];
	// 		$data['attr_id']=$_POST['attr_id'][$k];
	// 		$data['add_price']=(int)$_POST['add_price'][$k];
	// 		// p($data);exit;
	// 		$this->save($data);
	// 	}
	// 	return true;
	// }
	//

	public function addGoodsAttr($goods_id)
	{
		//最终插入到商品属性表的数据
		$insertData=array();
		foreach($_POST['attr_id'] as $k=>$v)
		{
			//属性值为空时不处理
			$_POST['attr_values'][$k] = trim($_POST['attr_values'][$k]);
			if(empty($_POST['attr_values'][$k]))
			{
				continue;
			}
			$data=array();
			$data['goods_id']=$goods_id;
			$data['attr_values']=$_POST['attr_values'][$k];
			$data['attr_id']=$_POST['attr_id'][$k];
			$data['add_price']=(int)$_POST['add_price'][$k];
			$where=array(
				'goods_id'=>$goods_id,
				'and attr_values'=>$data['attr_values'],
				'and attr_id'=>$data['attr_id']
				);
			//原有商品属性
			if($good_attr_id = Db::table('shop_goods_attr')->where($where)->pluck('good_attr_id'))
			{
				$data['good_attr_id']=$good_attr_id;
			}
			$insertData[]=$data;
		}

		Db::table('shop_goods_attr')->where('goods_id',$goods_id)->delete();

		foreach($insertData as $d)
		{
			$this->add($d);
		}
		return true;
	}

	public function goodstore()
	{
		$group = Db::table('shop_goods_attr sga')
		->join('shop_attr sa','sa.attr_id','=','sga.attr_id')
		->where('goods_id',Q('goods_id'))
		->where('attr_type',2)
		->groupBy('sga.attr_id')
		->get();


		$num = array();
		foreach ($group as $k => $v) {
			$num[] = Db::table('shop_goods_attr sga')
			->join('shop_attr sa','sa.attr_id','=','sga.attr_id')
			->where('goods_id',Q('goods_id'))
			->where('sga.attr_id',$v['attr_id'])
			->where('attr_type',2)
			->get();
		}
		// p($num);

		return $num;

	}

	public function getdatas()
	{
		$data = Db::table('shop_store ss')
		->where('goods_id',Q('get.goods_id'))
		->get();
		foreach ($data as $k => $v) {
			$format = array();
			$sp =preg_split('@_@',$v['mix_id']);
			foreach ($sp as $a => $b) {
				$format[].=Db::table('shop_goods_attr')->where('good_attr_id',$b)->pluck('attr_values');
				$form=implode('_',$format);
				$data[$k]['mix_id']=$form;
			}
		}

		return $data;
	}

	public function format_add()
	{
		$format = array();
		$_POST['attr']=$this->format();


		foreach ($_POST['attr'] as $key => $value) {
			$format['goods_id'] = Q('goods_id');
			$format['mix_id'] = $_POST['attr'][$key];
			$format['goods_num'] = $_POST['goods_num'][$key];
			$format['store_num'] = $_POST['store_num'][$key];


			if (!Db::table('shop_store')->where('mix_id',$format['mix_id'])->first()) {
				Db::table('shop_store')->insert($format);
			}
			else
			{
				continue;
			}

		}

	}

	private function format()
	{
		$format = array();


		/**
		 * 老师代码,供参考
		 */
		$count = count(current($_POST['attr']));
		for($i=0;$i<$count;$i++)
		{
			$tmp=array();
			foreach($_POST['attr'] as $v)
			{
				$tmp[]=$v[$i];
			}
			$format[]=implode('_',$tmp);
		}
		/**
		 * 老师代码,供参考
		 */

		return $format;
	}




}